package com.ry.demo.register.server;

import com.ry.demo.register.server.dto.HeartbeatRequest;
import com.ry.demo.register.server.dto.RegisterRequest;

import java.util.UUID;

/**
 *
 */
public class RegisterServer {

    public static void main(String[] args) throws InterruptedException {

        String serviceInstanceId = UUID.randomUUID().toString().replace("-", "");

        RegisterServerController controller = new RegisterServerController();

        RegisterRequest registerRequest = new RegisterRequest();
        registerRequest.setHostname("inventory-service-01");
        registerRequest.setIp("192.168.31.208");
        registerRequest.setPort(9000);
        registerRequest.setServiceInstanceId(serviceInstanceId);
        registerRequest.setServiceName("inventory-service");

        // A、模拟发起一个服务实例注册的请求
        controller.addInstance(registerRequest);


        HeartbeatRequest heartbeatRequest = new HeartbeatRequest();
        heartbeatRequest.setServiceName("inventory-service");
        heartbeatRequest.setServiceInstanceId(serviceInstanceId);

        // B、模拟进行一次心跳，完成续约
        controller.renew(heartbeatRequest);


        // C、开启一个心跳检测的后台线程
        ServiceAliveMonitor serviceAliveMonitor = new ServiceAliveMonitor();
        serviceAliveMonitor.start();

        // 避免jvm进程结束
//        while (true){
//            Thread.sleep(30 * 1000);
//        }

    }

}
